const colors = require('colors');

const {FFScene, FFImage, FFCreator} = require('ffcreator');

const video_creator = ({bg, img1, img2, outputDir, cacheDir}) => {
  // const audio = path.join(__dirname, './assets/audio/01.mp3');

  // 按照抖音 9：16 宽高比设置页面宽高
  const width = 1080;
  const height = 1920;
  // 创建视频入口
  const creator = new FFCreator({
    cacheDir,
    outputDir,
    width,
    height,
    debug: false,
    fps: 60,
    threads: 4,
    // 这里可以配置背景音乐
    // audio,
  });

  // 创建场景，当然一个可以创建多个场景，并增加转场动画
  const scene = new FFScene();
  scene.setBgColor('#000000');
	// 给场景加背景图片
  const fiBg = new FFImage({path: bg, x: width / 2, y: height / 2});
	// 创建图片，放在中心
  const fiImg1 = new FFImage({path: img1, x: width / 2, y: height / 2});
  const fiImg2 = new FFImage({path: img2, x: width / 2, y: height / 2});
  // 动画参考 https://animate.style/
  // 给图片添加入场出场动画三个参数分别是：动画名，持续时间，执行时机
  fiImg1.addEffect('zoomInDown', 1)
  fiImg1.addEffect('zoomOutDown', 1, 2)
  
  fiImg2.addEffect('zoomInDown', 1, 4);
  fiImg2.addEffect('zoomOutDown', 1, 5);
	// 将素材放入该场景
  scene.addChild(fiBg);
  scene.addChild(fiImg1);
  scene.addChild(fiImg2);
	// 该场景持续时间
  scene.setDuration(7);
  // 将场景放入该视频
  creator.addChild(scene);
  // 开始
  creator.start();
  creator.on('start', () => {
    console.log(`FFCreator start`);
  });

  creator.on('error', e => {
    console.log(`FFCreator error: ${e.error}`);
  });

  creator.on('progress', e => {
    console.log(colors.yellow(`FFCreator progress: ${e.state} ${(e.percent * 100) >> 0}%`));
  });

  creator.on('complete', e => {
    console.log(
      colors.magenta(`FFCreator completed: \n USEAGE: ${e.useage} \n PATH: ${e.output} `),
    );

    console.log(colors.green(`\n --- You can press the s key or the w key to restart! --- \n`));
  });

  return creator;
};

module.exports = video_creator